package dao.hibernate;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.math.NumberUtils;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.criterion.Example;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import vo.StuckEventVo;

/**
 * A data access object (DAO) providing persistence and search support for
 * StuckEventVo entities. Transaction control of the save(), update() and
 * delete() operations can directly support Spring container-managed
 * transactions or they can be augmented to handle user-managed Spring
 * transactions. Each of these methods provides additional information for how
 * to configure it for the desired type of transaction control.
 * 
 * @see vo.StuckEventVo
 * @author MyEclipse Persistence Tools
 */
public class StuckEventVoDAO extends BaseHibernateDAO {
	private static final Logger log = LoggerFactory
			.getLogger(StuckEventVoDAO.class);
	// property constants
	public static final String HOST_IP = "hostIp";
	public static final String DATE = "date";
	public static final String TIME = "time";
	public static final String STACKTRACE_UID = "stacktraceUid";
	public static final String CLIENT_IP = "clientIp";
	public static final String USER_ID = "userId";
	
	
	public List<StuckEventVo> findByHostIp(String hostIp, final String startDate,
			final String endDate) {
		try {
			String queryString = " from StuckEventVo where hostIp = ?  ";
			List<Object> parameter = new ArrayList<Object>();
			parameter.add(hostIp);
			if (NumberUtils.isDigits(startDate)) {
				queryString = queryString + " and CAST(date  AS int ) >= ? ";
				parameter.add(NumberUtils.toInt(startDate));
			}
			if (NumberUtils.isDigits(endDate)) {
				queryString = queryString + "and  CAST(date  AS int ) <= ? " ;
				parameter.add(NumberUtils.toInt(endDate));
			}
			final Query queryObject = getSession().createQuery(queryString);

			for (int i = 0; i < parameter.size(); ++i) {
				queryObject.setParameter(i, parameter.get(i));
			}

			List<StuckEventVo> result = queryObject.list();
			 
			return result;
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
	public int countByCauseUid(String hostIp, final String startDate,
			final String endDate) {
		log.debug("countByCauseUid");
		try {
			String queryString = "select  count(uid)  from StuckEventVo where hostIp = ?  ";
			List<Object> parameter = new ArrayList<Object>();
			parameter.add(hostIp);
			if (NumberUtils.isDigits(startDate)) {
				queryString = queryString + " and CAST(date  AS int ) >= ? ";
				parameter.add(NumberUtils.toInt(startDate));
			}
			if (NumberUtils.isDigits(endDate)) {
				queryString = queryString + "and  CAST(date  AS int ) <= ? " ;
				parameter.add(NumberUtils.toInt(endDate));
			}
			final Query queryObject = getSession().createQuery(queryString);

			for (int i = 0; i < parameter.size(); ++i) {
				queryObject.setParameter(i, parameter.get(i));
			}

			List<?> count = queryObject.list();
			if (count != null && count.size() > 0) {
				Long init = (Long) count.get(0);

				return NumberUtils.toInt(init.toString());
			}
			queryObject.list().size();
			return queryObject.list().size();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
	public int countByCauseUid(String hostIp, int uid, final String startDate,
			final String endDate) {
		log.debug("countByCauseUid");
		List<Object> parameter = new ArrayList<Object>();
		parameter.add(hostIp);
		parameter.add(String.valueOf(uid));
		try {
			String queryString = "select  count(uid)  from StuckEventVo where hostIp =? and stacktraceUid in (select uid from StackTraceVo where keyCause =?  ) ";
			if (NumberUtils.isDigits(startDate)) {
				queryString = queryString + " and CAST(date  AS int ) >= ? ";
				parameter.add(NumberUtils.toInt(startDate));
			}
			if (NumberUtils.isDigits(endDate)) {
				queryString = queryString + "and  CAST(date  AS int ) <= ? " ;
				parameter.add(NumberUtils.toInt(endDate));
			}
			final Query queryObject = getSession().createQuery(queryString);
			for (int i = 0; i < parameter.size(); ++i) {
				queryObject.setParameter(i, parameter.get(i));
			}
			
			List<?> count = queryObject.list();
			if (count != null && count.size() > 0) {
				Long init = (Long) count.get(0);

				return NumberUtils.toInt(init.toString());
			}
			queryObject.list().size();
			return queryObject.list().size();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public int countByCauseUid(String hostIp) {
		log.debug("countByCauseUid");
		try {
			String queryString = "select  count(uid)  from StuckEventVo where hostIp =?  ";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, hostIp);
			List<?> count = queryObject.list();
			if (count != null && count.size() > 0) {
				Long init = (Long) count.get(0);

				return NumberUtils.toInt(init.toString());
			}
			queryObject.list().size();
			return queryObject.list().size();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	

	public void save(StuckEventVo transientInstance) {
		log.debug("saving StuckEventVo instance");
		try {
			getSession().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(StuckEventVo persistentInstance) {
		log.debug("deleting StuckEventVo instance");
		try {
			getSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public StuckEventVo findById(java.lang.Integer id) {
		log.debug("getting StuckEventVo instance with id: " + id);
		try {
			StuckEventVo instance = (StuckEventVo) getSession().get(
					"vo.StuckEventVo", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List<?> findByExample(StuckEventVo instance) {
		log.debug("finding StuckEventVo instance by example");
		try {
			List<?> results = getSession().createCriteria("vo.StuckEventVo")
					.add(Example.create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List<StuckEventVo> findByProperty(String propertyName, Object value) {
		log.debug("finding StuckEventVo instance with property: "
				+ propertyName + ", value: " + value);
		try {
			String queryString = "from StuckEventVo as model where model."
					+ propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List<StuckEventVo> findByHostIp(Object hostIp) {
		return findByProperty(HOST_IP, hostIp);
	}

	public List<?> findByDate(Object date) {
		return findByProperty(DATE, date);
	}

	public List<?> findByTime(Object time) {
		return findByProperty(TIME, time);
	}

	public List<?> findByStacktraceUid(Object stacktraceUid) {
		return findByProperty(STACKTRACE_UID, stacktraceUid);
	}

	public List<?> findByClientIp(Object clientIp) {
		return findByProperty(CLIENT_IP, clientIp);
	}

	public List<?> findByUserId(Object userId) {
		return findByProperty(USER_ID, userId);
	}

	public List<String> findDistinctHostIp() {
		log.debug("finding all StuckEventVo instances");
		try {
			String queryString = "select distinct vo.hostIp from StuckEventVo vo order by  vo.hostIp ";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public List<?> findAll() {
		log.debug("finding all StuckEventVo instances");
		try {
			String queryString = "from StuckEventVo";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public StuckEventVo merge(StuckEventVo detachedInstance) {
		log.debug("merging StuckEventVo instance");
		try {
			StuckEventVo result = (StuckEventVo) getSession().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(StuckEventVo instance) {
		log.debug("attaching dirty StuckEventVo instance");
		try {
			getSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(StuckEventVo instance) {
		log.debug("attaching clean StuckEventVo instance");
		try {
			getSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
}